package it.koutifaris.travianmap.mapsqlparser;

import static it.koutifaris.testforclassutils.IsTestForAssert.assertIsTestFor;
import it.koutifaris.travianmap.TestRemoteSource;
import it.koutifaris.travianmap.mapsqlparser.MapSqlParser.RemoteSource;

import java.io.InputStream;
import java.util.Date;

import junit.framework.Assert;
import android.test.AndroidTestCase;

public class ProgressHandlerTest extends AndroidTestCase {

	private static final String TEST_FILE_NAME = "map.sql";
	private static final int TEST_FILE_SIZE = 1196;
	private TestParser parser;
	private RemoteSource testSourceStream;

	@Override
	protected void setUp() throws Exception {
		parser = new TestParser();
		InputStream stream = getClass().getResourceAsStream(TEST_FILE_NAME);
		testSourceStream = new TestRemoteSource(new Date(), stream, false);

	}

	public void testProgress() {
		TestMapSqlEntryHandler testMapSqlEntryHandler = new TestMapSqlEntryHandler();
		parser.parse(testSourceStream, testMapSqlEntryHandler, testMapSqlEntryHandler);
	}

	public void testPkgAndName() {
		assertIsTestFor(ProgressHandler.class);
	}

	private static class TestParser extends AbstractMapSqlParser {

		@Override
		protected MapSqlEntry parseLine(String line) throws Exception {
			return new MapSqlEntry();
		}

	}

	private static class TestMapSqlEntryHandler implements MapSqlEntryHandler, ProgressHandler {

		private long internalByteCount;

		@Override
		public void init(Date lastModified) {
		}

		@Override
		public boolean onEntry(MapSqlEntry entry) {
			return true;
		}

		@Override
		public void onFail(Exception e) {
		}

		@Override
		public void done() {
			Assert.assertEquals(TEST_FILE_SIZE, internalByteCount);
		}

		@Override
		public void onProgress(long byteCount) {
			this.internalByteCount = byteCount;
		}

		@Override
		public void onFinish(long byteCount) {
			Assert.assertEquals(TEST_FILE_SIZE, byteCount);
		}

		@Override
		public void init(long contentLength) {
			Assert.assertEquals(TEST_FILE_SIZE, contentLength);
		}

	}
}
